/*
 * HTTPDownloader.java
 *
 * Created on 12 October 2007, 15:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fecchi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLConnection;

/**
 *
 * @author rcc4
 */
public class HTTPDownloader implements Downloader {
    
    TransferRequest localRequest;
    private URLConnection connection;
    
    /**
     * Creates a new instance of HTTPDownloader
     */
    public HTTPDownloader() {
        System.out.println("HTTP Downloader Instantiated");
        localRequest = null;
    }
    
    public void prep(TransferRequest request) throws DownloaderException{
        localRequest = request;
        
    }
    
    public void run() {
        BufferedReader in;
        String inputLine;
        try {
            connection = localRequest.getUrl().openConnection(localRequest.getProxy());
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            
            
            while ((inputLine = in.readLine()) != null)
                System.out.println(inputLine);
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.localRequest.setStatus("Finished");
    }
    
    public synchronized TransferRequest getTransferRequest() {
        return localRequest;
    }
    
    
}
